
package com.idealighter.game.roomcard.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.idealighter.game.core.common.Game;
import com.idealighter.game.games.common.AbstractSeat;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.player.manager.PlayerMsgWriter;
import com.idealighter.game.player.struct.Player;
import com.idealighter.game.roomcard.bo.RoomCardRoomListDomainBo;
import com.idealighter.game.roomcard.games.mustniuniu.struct.MustNiuNiuTable;
import com.idealighter.game.roomcard.message.ResEnterRoomCardMsg;
import com.idealighter.game.roomcard.message.ResRoomCardListMsg;
import com.idealighter.game.roomcard.message.ResRoomCardListMsg.RoomInfo;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardRoomListDomain;
import com.idealighter.utils.check.EmptyUtil;
import java.util.ArrayList;
import java.util.List;

/**
 * 四人牛牛消息管理.
 * 
 * @date 2016年1月20日 下午4:46:02
 *
 */
@Singleton
public class RoomCardMsgMgr {
  @Inject
  private PlayerMsgWriter msgWriter;

  /**
   * @Title enterRoomCardMsg.
   * @Description 返回进入房卡信息
   * @date 2018年6月8日 下午1:48:41
   * @return .
   */
  public ResEnterRoomCardMsg enterRoomCardMsg(Player player, Game game, boolean needPwd) {
    ResEnterRoomCardMsg msg = new ResEnterRoomCardMsg();
    msg.setGameId(game.getType());
    msg.setNeedPwd(needPwd ? 1 : 0);

    return msg;
  }

  /**
   * @Title roomCardListMsg.
   * @Description 返回房卡列表消息
   * @date 2018年6月8日 下午1:49:12
   * @return .
   */
  public ResRoomCardListMsg roomCardListMsg(Player player,
      List<RoomCardRoomListDomainBo> roomDomains) {
    ResRoomCardListMsg msg = new ResRoomCardListMsg();
    if (EmptyUtil.listIsNotEmpty(roomDomains)) {
      List<RoomInfo> roomList = new ArrayList<RoomInfo>();
      for (RoomCardRoomListDomainBo domainPo : roomDomains) {
        RoomCardRoomListDomain domain = domainPo.getDomain();
        RoomInfo resultItem = new RoomInfo();
        resultItem.setRoomId(domain.getId());
        resultItem.setOwnerId(domain.getPlayerId());
        resultItem.setOwnerName(domainPo.getOwnerName());
        resultItem.setOwnerAvatar(domainPo.getDomain().getPlayerAvatar());
        resultItem.setSuperId(domain.getSuperId());
        resultItem.setGroupId(domain.getGroupId());
        resultItem.setGameId(domain.getGameId());
        resultItem.setGameName(domain.getGameName());
        resultItem.setPayBy(domain.getPayBy());
        resultItem.setPrice(domain.getPrice());
        resultItem.setGameCfg(domain.getGameCfg());
        resultItem.setRounds(domain.getPlayGames() + domain.getLeftGame());
        resultItem.setCreateTime(domain.getCreateTime().getTime());
        resultItem.setPlayerNum(domainPo.getPlayerNum());
        roomList.add(resultItem);
      }
      msg.setRoomList(roomList);
    }
    return msg;
  }

  /**
   * 向同桌的玩家(包括自己)发送消息.
   * 
   * @param table .
   * @param msg .
   */
  public void sendTablePlayersMsg(MustNiuNiuTable table, ResMessage msg) {
    List<? extends AbstractSeat> seats = table.seats();
    for (int i = 0; i < seats.size(); i++) {
      AbstractSeat seat = seats.get(i);
      long playerId = seat.getPlayerId();
      if (playerId > 0) {
        msgWriter.writeMsg(playerId, msg);
      }
    }
  }
}
